/**
* 结算管理
*/
import React, { Component } from 'react';
import { View, StyleSheet, Text, Image, TouchableOpacity, FlatList, TextInput } from "react-native";
import { connect } from 'react-redux';
import { create_service } from '../../redux/index.js';
import { Contract } from '../../service/index.js';
import { Colors, Dimens, ImageRes } from '../../utils/Assets';
import { ProgressView } from '../custom/index'
import { Actions } from 'react-native-router-flux';
import { getStore } from '../../redux/index.js';
class TeamBalanceView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            refreshing: false,
            team_list: []
        }
        this.pageSize = 10
        this.page = 1
        this.userInfo = getStore().getState().auth.userInfo
    }

    componentDidMount() {
        this.getDataList(true)
    }
    componentWillUnmount() {
    }
    getDataList(refresh) {
        if (refresh) {
            this.setState({ refreshing: true })
        } else {
            this.setState({ loading: true })
        }
        let params = {
            page: this.page,
            pageSize: this.pageSize,
            token: this.userInfo.token
        }
        this.props.dispatch(create_service(Contract.POST_USER_DIRECT_CHILD, params)).then((res) => {
            if (res) {
                if (refresh) {
                    this.setState({ team_list: res.list, pagination: res.pagination, loading: false, refreshing: false })
                } else {
                    this.setState({ team_list: this.state.team_list.concat(res.list), pagination: res.pagination, loading: false, refreshing: false })
                }
            } else {
                this.setState({ loading: false, refreshing: false })
            }
        })
    }
    onEndReached() {
        if (this.state.team_list.length != 0 && this.state.team_list.length != this.state.pagination.total) {
            this.page += 1
            this.getDataList(false)
        }
    }
    renderItem({ item, index }) {
        return (
            <TouchableOpacity style={{ backgroundColor: "#ffffff", padding: 15, flexDirection: 'row', justifyContent: 'space-between', borderRadius: 8, marginTop: 15, marginHorizontal: 15 }} activeOpacity={0.8} onPress={() => {
                Actions.teamRateView({ real_name: item.real_name, mobile: item.mobile, users_id: item.id })
            }}>
                <View style={{}}>
                    <Text style={{ fontSize: 15, color: "#333333", fontWeight: 'bold' }}>{item.real_name}</Text>
                    <Text style={{ fontSize: 14, color: "#999999", marginTop: 15 }}>{"注册时间：" + item.create_time}</Text>
                </View>
                <Image source={ImageRes.team_right_arrow} style={{ width: 10, height: 16, alignSelf: 'center', marginLeft: 10 }} />
            </TouchableOpacity>
        )
    }
    render() {
        return (
            <View style={styles.container}>
                <FlatList
                    ref={(list) => (this.flatList = list)}
                    keyExtractor={(item, index) => index.toString()}
                    data={this.state.team_list}
                    ItemSeparatorComponent={() => <View style={{ backgroundColor: Colors.bg_color, height: 1 }} />}
                    showsVerticalScrollIndicator={false}
                    renderItem={this.renderItem.bind(this)}
                    refreshing={this.state.refreshing}
                    onEndReachedThreshold={0.1}
                    onRefresh={() => {
                        this.page = 1
                        this.getDataList(true)
                    }}
                    onEndReached={() => {
                        this.onEndReached();
                    }}
                />
                <ProgressView show={this.state.loading} />
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: Colors.bg_color,
    }
});
const ExportView = connect()(TeamBalanceView);
module.exports.TeamBalanceView = ExportView
